//---------------------------------------------------------------------------
#pragma hdrstop
#include "ColoredArcClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
void CColoredArc::showOutlines() {
	canvas->Pen->Color = main_clNFont;
	int tmp = canvas->Font->Size;
	canvas->MoveTo(pts[0].x, pts[0].y);
	for (int i = 1; i < 4; i++)
		if (!(outlinescount == 2 && i == 2))
			canvas->LineTo(pts[i].x, pts[i].y);
	canvas->Ellipse(pts[1].x - 10, pts[1].y - 10, pts[1].x + 10, pts[1].y + 10);
	if (outlinescount == 3)
		canvas->Ellipse(pts[2].x - 10, pts[2].y - 10, pts[2].x + 10, pts[2].y + 10);
	if (outlinescount == 3) {
		canvas->Font->Size = 8;
		canvas->TextOutA(pts[2].x - 3, pts[2].y - 8, "2");
		canvas->TextOutA(pts[1].x - 3, pts[1].y - 8, "1");
		canvas->Font->Size = tmp;
	}
}
//---------------------------------------------------------------------------
CColoredArc::CColoredArc(TCanvas* canvas) {
	this->canvas = canvas;
	canvas->Font->Name = main_font;
	canvas->Font->Color = main_clFont;
	outlines = false;
	outlinescount = 2;
	for (int i = 0; i < 4; i++) {
		pts[i].x = 0;
		pts[i].y = 0;
	}
}
//---------------------------------------------------------------------------
void CColoredArc::setPoint(int index, int x, int y) {
	if (index <= 3 && index >= 0) {
		pts[index].x = x;
		pts[index].y = y;
	}
}
//---------------------------------------------------------------------------
void CColoredArc::setPoints(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
	pts[0].x = x0;
	pts[0].y = y0;
	pts[1].x = x1;
	pts[1].y = y1;
	pts[2].x = x2;
	pts[2].y = y2;
	pts[3].x = x3;
	pts[3].y = y3;
}
//---------------------------------------------------------------------------
void CColoredArc::setDx(double dx) {
	if (dx > 0 && dx < 1)
		this->dx = dx;
}
//---------------------------------------------------------------------------
void CColoredArc::setOutlines(bool outlines) {
	this->outlines = outlines;
}
//---------------------------------------------------------------------------
void CColoredArc::setOutlinesCount(int outlinescount) {
	if (outlinescount == 2 || outlinescount == 3)
		this->outlinescount = outlinescount;
}
//---------------------------------------------------------------------------
void CColoredArc::setIndexes(int ip, int it) {
	this->ip = ip;
	this->it = it;
}
//---------------------------------------------------------------------------
void CColoredArc::setDirection(bool direction) {
	this->direction = direction;
}
//---------------------------------------------------------------------------
int CColoredArc::getX(int index) {
	if (index <= 3 && index >= 0)
		return pts[index].x;
	else
		return -1;
}
//---------------------------------------------------------------------------
void CColoredArc::setRules(vector <rule> rules) {
	this->rules.clear();
	for (int i = 0; i < rules.size(); i++) {
		this->rules.push_back(rules[i]);

    }
}
//---------------------------------------------------------------------------
int CColoredArc::getY(int index) {
	if (index <= 3 && index >= 0)
		return pts[index].y;
	else
		return -1;
}
//---------------------------------------------------------------------------
int CColoredArc::getPositionIndex() {
	return ip;
}
//---------------------------------------------------------------------------
int CColoredArc::getTransitionIndex() {
	return it;
}
//---------------------------------------------------------------------------
int CColoredArc::getRestPoint(int xm, int ym) {
	for (int i = 1; i < 3; i++)
		if (outlines)
			if (xm >= pts[i].x - 10 && xm <= pts[i].x + 10 && ym >= pts[i].y - 10 && ym <= pts[i].y + 10)
				return i;
	return 0;
}
//---------------------------------------------------------------------------
bool CColoredArc::getOutlines() {
	return outlines;
}
//---------------------------------------------------------------------------
int CColoredArc::getOutlinesCount() {
	return outlinescount;
}
//---------------------------------------------------------------------------
bool CColoredArc::getDirection() {
	return direction;
}
vector <rule> CColoredArc::getRules() {
	return rules;
}
//---------------------------------------------------------------------------
void CColoredArc::show() {
	double t, xt, yt, tmp = 0;
	if (outlines)
		showOutlines();
	if (outlinescount == 3)
		for (int i = 0; i < 1 / dx; i++) {
			t = i * dx;
			xt = pts[0].x * (1 - t) * (1 - t) * (1 - t) + pts[1].x * 3 * t * (1 - t) * (1 - t) + pts[2].x * 3 * t * t * (1 - t) + pts[3].x * t * t * t;
			yt = pts[0].y * (1 - t) * (1 - t) * (1 - t) + pts[1].y * 3 * t * (1 - t) * (1 - t) + pts[2].y * 3 * t * t * (1 - t) + pts[3].y * t * t * t;
			if (i >= 1 / dx / 3 * 2)
				if (tmp < 255)
					canvas->Pixels[xt][yt] = RGB(tmp++, 0, 0);
				else
					canvas->Pixels[xt][yt] = RGB(tmp, 0, 0);
			else
				canvas->Pixels[xt][yt] = main_clBorder;
		}
	if (outlinescount == 2)
		for (int i = 0; i < 1 / dx; i++) {
			t = i * dx;
			xt = pts[0].x * (1 - t) * (1 - t) + pts[1].x * 2 * t * (1 - t) + pts[3].x * t * t;
			yt = pts[0].y * (1 - t) * (1 - t) + pts[1].y * 2 * t * (1 - t) + pts[3].y * t * t;
			if (i >= 1 / dx / 3 * 2)
				if (tmp < 255)
					canvas->Pixels[xt][yt] = RGB(tmp++, 0, 0);
				else
					canvas->Pixels[xt][yt] = RGB(tmp, 0, 0);
			else
				canvas->Pixels[xt][yt] = main_clBorder;
		}
}
//---------------------------------------------------------------------------
bool CColoredArc::isSelect(int xm, int ym) {
	const int R = 3;
	double t, xt, yt;
	if (outlinescount == 3)
		for (int i = 0; i < 1 / dx; i++) {
			t = i * dx;
			xt = pts[0].x * (1 - t) * (1 - t) * (1 - t) + pts[1].x * 3 * t * (1 - t) * (1 - t) + pts[2].x * 3 * t * t * (1 - t) + pts[3].x * t * t * t;
			yt = pts[0].y * (1 - t) * (1 - t) * (1 - t) + pts[1].y * 3 * t * (1 - t) * (1 - t) + pts[2].y * 3 * t * t * (1 - t) + pts[3].y * t * t * t;
			if (xm >= xt - R && xm <= xt + R && ym >= yt - R && ym <= yt + R && !outlines)
				return true;
		}
	if (outlinescount == 2)
		for (int i = 0; i < 1 / dx; i++) {
			t = i * dx;
			xt = pts[0].x * (1 - t) * (1 - t) + pts[1].x * 2 * t * (1 - t) + pts[3].x * t * t;
			yt = pts[0].y * (1 - t) * (1 - t) + pts[1].y * 2 * t * (1 - t) + pts[3].y * t * t;
			if (xm >= xt - R && xm <= xt + R && ym >= yt - R && ym <= yt + R && !outlines)
				return true;
		}
	return false;
}
//---------------------------------------------------------------------------
UnicodeString CColoredArc::save() {
	int size = rules.size();
	UnicodeString str = "";
	str += IntToStr(outlinescount) + " ";
	for (int i = 0; i < 4; i++)
		str += IntToStr(pts[i].x) + " " + IntToStr(pts[i].y) + " ";
	str += IntToStr(size) + " ";
	for (int i = 0; i < size; i++)
		if (i == size - 1)
			str += IntToStr(rules[i].color) + " " + IntToStr(rules[i].markcount);
		else
			str += IntToStr(rules[i].color) + " " + IntToStr(rules[i].markcount) + " ";
	return str;
}
//---------------------------------------------------------------------------
